package com.burgeon.backend.SHOP.rest;
/**
 * 按商品条码
 * 查找评论
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.Comment;
import com.burgeon.backend.entity.RetailRecord;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindComment extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindComment(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<Comment> getData(String gcode) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(gcode),url);
        System.err.println(str);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(code)){
            throw new Exception(message);
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Comment> data = new ArrayList();
        for (int i =0;i<jsonArray.length();i++){
            JSONArray array = jsonArray.getJSONArray(i);
            Comment comment = new Comment();
            comment.setId(array.getString(0));
            comment.setComment(array.getString(1));
            comment.setVip(array.getString(2));
            comment.setScore1(array.getString(3));
            comment.setScore2(array.getString(4));
            comment.setScore3(array.getString(5));
            comment.setScore4(array.getString(6));
            String time = array.getString(7);
            comment.setCreateTime(time.substring(0,time.length()-2));
            data.add(comment);
        }

        return data;
    }

    private MultiValueMap Data(String code) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("DESCRIPTION");//评论
        columns.add("'C_VIP_ID;CARDNO'");//卡号
        columns.add("'PRODUCT'");//质量水平满意度
        columns.add("'COMFORTABLE'");//服务水平满意度
        columns.add("'PACKING'");//购物环境满意度
        columns.add("'DELIVERY_SPEED'");//购物环境满意度
        columns.add("CREATIONDATE");//时间

        params.put("columns", columns);
        params.put("table", "YC_VALUATION");//表名

        params.put("count","true");

        HashMap orderby = new HashMap();

        orderby.put("column","CREATIONDATE");
        orderby.put("asc","false");

        /*HashMap paramss = new HashMap();
        String ac = "'"+ code+"'";
        paramss.put("column", "'M_PRODUCT_ID;NAME'");
        paramss.put("condition", ac);*/



        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        String ac = "'=" + code + "'";
        expr1.put("column", "'M_PRODUCT_ID;NAME'");
        expr1.put("condition", ac);
        HashMap expr2 = new HashMap();
        expr2.put("column", "'IS_SHOW'");
        expr2.put("condition", "'Y'");

        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);
        params.put("params", paramss);

        return DATA("Query", params,account,password);
    }
    public static void main(String[] args) {
        FindComment getRetailByVip = new FindComment("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(getRetailByVip.getData("BDL9S776"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
